package com.mb.app.appused.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;

/**
 * 网络加载图片
 * @author wo2app
 * @email wo2app@qq.com
 * @time 2015年1月31日
 * @version Vx.x.x
 * @function
 */
public class ImageClient {

	Context context;
	ImageView srcView;
	String urlPath;
	int TYPE=0;   //0 src   1 banckground

	public ImageClient(Context context) {
		this.context = context;
	}

	public ImageClient(Context context, int tag) {
		this.context = context;
	}

	public ImageClient(Context context, ImageView view, String src) {
		this.context = context;
		this.srcView = view;
		this.urlPath = src;
	}
	public ImageClient(Context context, ImageView view, String src,int type) {
		this.context = context;
		this.srcView = view;
		this.urlPath = src;
		this.TYPE=type;
		
	}

	public void execute() {
		if (this.srcView != null && this.urlPath != null) {
			CacheTask task = new CacheTask(this.srcView, this.urlPath);
			task.execute();
		}
	}

	private class CacheTask extends AsyncTask<Void, Integer, Integer> {

		Bitmap bitmap = null;
		String url = null;
		ImageView view = null;

		public CacheTask(ImageView view, String src) {
			this.url = src;
			this.view = view;
		}

		@Override
		protected Integer doInBackground(Void... params) {
			// TODO Auto-generated method stub
			boolean RUN = true;
			while (RUN) {
				try {
					Thread.sleep(2000);
					RUN = loadNet(this.url);
				} catch (InterruptedException e) {
				}
			}
			return null;
		}

		// 执行中
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		// 处理完成
		@SuppressWarnings("deprecation")
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub

			if (this.bitmap != null && this.view != null && this.url != null) {
				if(TYPE==0){
					this.view.setImageDrawable(toDrawalbe(this.bitmap));
				}else{
					this.view.setBackgroundDrawable(toDrawalbe(this.bitmap));
				}
					
			}
			super.onPostExecute(result);
		}

		// 加载网络
		private boolean loadNet(String path) {
			try {
				URL filURL = new URL(path);
				HttpURLConnection conn = (HttpURLConnection) filURL
						.openConnection();
				conn.setConnectTimeout(6000);
				conn.setDoInput(true);
				conn.setUseCaches(false);
				InputStream is = conn.getInputStream();
				// 解析得到图片
				this.bitmap = BitmapFactory.decodeStream(is);
				// 关闭数据流
				is.close();
				return false;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		}

		// 转换图片 压缩图片处理
		@SuppressWarnings("deprecation")
		private Drawable toDrawalbe(Bitmap bit) {
			Bitmap bitmap = bit;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
			int options = 100;
			while (baos.toByteArray().length / 1024 > 30) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
				baos.reset();// 重置baos即清空baos
				bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%，把压缩后的数据存放到baos中
				options -= 10;// 每次都减少10
			}
			ByteArrayInputStream isBm = new ByteArrayInputStream(
					baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
			bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
			Drawable drawable = new BitmapDrawable(bitmap);
			System.gc();
			return drawable;
		}

	}

}
